package cos.gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import com.qotsa.exception.InvalidHandle;
import com.qotsa.jni.controller.WinampController;

@SuppressWarnings("serial")
public class NavigationPanel extends JPanel {
	private final JButton prevButton;
	private final JButton playButton;
	private final JButton pauseButton;
	private final JButton stopButton;
	private final JButton nextButton;
	private final Button_Handler handler = new Button_Handler();
	private final Dimension buttonDimension = new Dimension(20, 20);

	public NavigationPanel() {
		// set data in buttons
		prevButton = setButton(0);
		playButton = setButton(1);
		pauseButton = setButton(2);
		stopButton = setButton(3);
		nextButton = setButton(4);

		// stupid hotkeys
		prevButton.setMnemonic(java.awt.event.KeyEvent.VK_Z);
		playButton.setMnemonic(java.awt.event.KeyEvent.VK_X);
		pauseButton.setMnemonic(java.awt.event.KeyEvent.VK_C);
		stopButton.setMnemonic(java.awt.event.KeyEvent.VK_V);
		nextButton.setMnemonic(java.awt.event.KeyEvent.VK_B);

		// what we see if our curcor is on the button
		prevButton.setToolTipText("Previous Track [Alt+Z]");
		playButton.setToolTipText("Play [Alt+X]");
		pauseButton.setToolTipText("Pause [Alt+C]");
		stopButton.setToolTipText("Stop [Alt+V]");
		nextButton.setToolTipText("Next Track [Alt+B]");
	}

	/**
	 * sets data and listener to buttons
	 */
	private JButton setButton(int i) {
		try {
			JButton button = new JButton(new ImageIcon(ImageIO.read(new File(
					"img/btn/button" + i + ".png"))));
			button.addActionListener(handler);
			button.setPreferredSize(buttonDimension);
			add(button);
			return button;
		} catch (IOException e) {
		}
		return null;
	}

	class Button_Handler implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			try {
				if (evt.getSource() == prevButton) {
					WinampController.previousTrack();
				} else if (evt.getSource() == playButton) {
					WinampController.play();
				} else if (evt.getSource() == pauseButton) {
					WinampController.pause();
				} else if (evt.getSource() == stopButton) {
					WinampController.stop();
				} else if (evt.getSource() == nextButton) {
					WinampController.nextTrack();
				}
			} catch (InvalidHandle e) {
			}
		}
	}

}
